const MapCtrl = {
    initMap() {
        minemap.domainUrl = '//minedata.cn';
        minemap.spriteUrl = '//minedata.cn/minemapapi/v1.3/sprite/sprite';
        minemap.serviceUrl = '//minedata.cn/service';
        minemap.accessToken = 'a3622f2702c34957a160242d2eb0e508';
        minemap.solution = 3756;
        minemap.TRW = {
            stayStatic: true
        };

        let map = new minemap.Map({
            container: 'map',
            style: "//minedata.cn/service/solu/style/id/3756",
            center: [109.3667628, 35.508069],
            zoom: 10,   //12
            pitch: 0,
            maxZoom: 17,
            minZoom: 5,  //10
            logoControl: false
        });

        map.solution = 3756;
        map.repaint = true;

        return map;
    },


    initWeatherSource(map) {


        map.addSource("warning", {
            "type": "vector",
            'tiles': ['http://203.81.245.38:48082/weather/weatheralarm/{z}/{x}/{y}']
        });


        map.addSource("weather-all", {
            "type": "vector",
            'tiles': ['http://203.81.245.38:48082/weather/weatherall/{z}/{x}/{y}?city=001001']
        });

        // map.addSource("weather-pre", {   //降水
        //     "type": "vector",
        //     'tiles': ['http://203.81.245.38:48082/weather/weatherpre/{z}/{x}/{y}']
        // });

        // map.addSource("weather-vism", {   //能见度
        //     "type": "vector",
        //     'tiles': ['http://203.81.245.38:48082/weather/weathervism/{z}/{x}/{y}']
        // });

        // map.addSource("weather-tem", {   //温度
        //     "type": "vector",
        //     'tiles': ['http://203.81.245.38:48082/weather/weathertem/{z}/{x}/{y}']
        // });

        // map.addSource("weather-winds", {   //风力
        //     "type": "vector",
        //     'tiles': ['http://203.81.245.38:48082/weather/weatherwinds/{z}/{x}/{y}']
        // });



        //定时器
        // MapCtrl.setTimeout(function () {
        //     map.removeSource();
        //     map.addSource();
        // },600000)

    },

    initWeatherLayers(map, backgroundId, pid) {
        // MapCtrl._initWeatherVismLayers(map, backgroundId, pid);
        // MapCtrl._initPreLayers(map, backgroundId, pid);
        // MapCtrl._initTemLayers(map, backgroundId, pid);
        // MapCtrl._initWindsLayers(map, backgroundId, pid);
        
        MapCtrl._initWarnLayers(map, backgroundId, pid);
        MapCtrl._initStyleLayers(map, backgroundId, pid, 'winds', 'Winds', [
            [10.8, '#28AD86'],
            [10.9, '#F8CA37'],
            [20.8, '#F8CA37'],
            [20.9, '#FE4B3C'],
            [28.5, '#FE4B3C'],
            [28.6, '#28AD86']
        ], "weather-all", "RoadWeatherAll");
        MapCtrl._initStyleLayers(map, backgroundId, pid, 'tem', 'Tem', [
            [-30, '#28AD86'],
            [-29.5, '#FE4B3C'],
            [-15, '#FE4B3C'],
            [-14.9, '#F8CA37'],
            [-5, '#F8CA37'],
            [-4.9, '#28AD86']
        ], "weather-all", "RoadWeatherAll");
        MapCtrl._initStyleLayers(map, backgroundId, pid, 'pre', 'Pre', [
            [2.0, '#28AD86'],
            [2.01, '#F8CA37'],
            [4.0, '#F8CA37'],
            [4.01, '#FE4B3C'],
            [8.0, '#FE4B3C'],
            [8.01, '#28AD86']
        ], "weather-all", "RoadWeatherAll");
        MapCtrl._initStyleLayers(map, backgroundId, pid, 'vism', 'Vism', [
            [0.5, '#28AD86'],
            [0.51, '#FE4B3C'],
            [1, '#FE4B3C'],
            [1.01, '#F8CA37'],
            [10, '#F8CA37'],
            [10.01, '#28AD86']
        ], "weather-all", "RoadWeatherAll");
    },

    _initWindsLayers(map, backgroundId, pid) {
        map.addLayer({
            "id": "winds",                //rh
            "type": "line",
            "source": "weather-all",            //weather
            "source-layer": "RoadWeatherAll",  //RoadWeather
            "layout": {
                "line-join": "round",
                "line-cap": "round",
                // 'visibility': 'none'    //
            },
            "paint": {
                "line-color": {
                    "property": "Winds",         //RH
                    "stops": [
                        // [0, '#28AD86'],
                        // [10, '#28AD86'],
                        // [10.1, '#E0B62C'],
                        // [20.8, '#E0B62C'],
                        // [20.9, '#FE4B3C'],
                        // [28.5, '#FE4B3C'],
                        // [28.6, '#8E71FF'],
                        [10.8, '#28AD86'],
                        [20.8, '#F8CA37'],
                        [28.5, '#FE4B3C'],
                        [100, '#A189FF']
                    ]
                },
                "line-width": 3
            }
        }, backgroundId);
    },

    _initTemLayers(map, backgroundId, pid) {
        map.addLayer({
            "id": "tem",                //rh
            "type": "line",
            "source": "weather-all",            //weather
            "source-layer": "RoadWeatherAll",  //RoadWeather
            "layout": {
                "line-join": "round",
                "line-cap": "round",
                // 'visibility': 'none'    //
            },
            "paint": {
                "line-color": {
                    "property": "Tem",         //RH
                    "stops": [
                        // [0, '#28AD86'],
                        // [10, '#28AD86'],
                        // [10.1, '#E0B62C'],
                        // [20.8, '#E0B62C'],
                        // [20.9, '#FE4B3C'],
                        // [28.5, '#FE4B3C'],
                        // [28.6, '#8E71FF'],
                        [-30, '#FE4B3C'],
                        [-15, '#F8CA37'],
                        [-5, '#28AD86'],
                        [100, '#28AD86']
                    ]
                },
                "line-width": 3
            }
        }, backgroundId);
    },

    _initWarnLayers(map, backgroundId, pid) {
        // map.addLayer({
        //     "id": "warn",
        //     "type": "symbol",
        //     'source': 'warning',
        //     "source-layer": "WeatherAlarm",
        //     "layout": {
        //         "icon-image": "marker-15-6",
        //         "icon-allow-overlap": true,  //图标允许压盖
        //     },
        //     "paint": {
        //         'icon-color': {
        //             "property": "alarm_lev",
        //             "stops": [
        //                 [1, '#0000FF'],
        //                 [2, '#FFFF00'],
        //                 [3, '#FFA500'],
        //                 [4, '#FF0000']
        //             ]
        //         },
        //     },
        //     "minzoom": 3,
        //     "maxzoom": 17,
        // });
        map.addLayer({
            "id": "warn",
            "type": "symbol",
            'source': 'warning',
            "source-layer": "WeatherAlarm",
            "layout": {
                'visibility': 'none',
                "icon-image": "{alarm_lev}",
                "icon-allow-overlap": true,  //图标允许压盖
                "symbol-avoid-edges": true
            },
            "minzoom": 3,
            "maxzoom": 17,
        });
    },

    _initPreLayers(map, backgroundId, pid) {

        map.addLayer({
            "id": "pre",
            "type": "line",
            "source": "weather-all",
            "source-layer": "RoadWeatherAll",
            "layout": {
                "line-join": "round",
                "line-cap": "round"
            },
            "paint": {
                "line-color": {
                    "property": "Pre",
                    "stops": [
                        [0.3, '#28AD86'],
                        [0.8, '#F8CA37'],
                        [2.0, '#FE4B3C'],
                        [1000, '#A189FF']
                    ]
                },
                "line-width": 3
            }
        }, backgroundId);


    },

    _initRoadCondLayers(map, backgroundId, pid) {
        const roadCondLineWidths = [{"stops": [[5, 1], [18, 4]], "base": 1.2}, {
            "stops": [[5, 1], [18, 3]],
            "base": 1.2
        }, {"stops": [[5, 1], [18, 2.5]], "base": 1.2}, {"stops": [[5, 1], [18, 2]], "base": 1.2}]
        const roadCondFilters = [["in", "function_class", 1, 2], ["==", "function_class", 3], ["==", "function_class", 4], ["==", "function_class", 5]];
        const roadCondMaxZooms = [17.5, 17.5, 17.5, 17.5];
        const roadCondMinZooms = [6.0, 11.0, 13.0, 14.0];

        for (let i = 0; i < 4; i++) {
            map.addLayer({
                "id": "road-cond-" + parseInt(i + 1),
                "type": "line",
                "source": "HistXian",
                "source-layer": "HistXian",
                "maxzoom": roadCondMaxZooms[i],
                "minzoom": roadCondMinZooms[i],
                "layout": {
                    "line-join": "round",
                    "line-cap": "round",
                    "visibility": "visible",
                },
                "paint": {
                    "line-color": {
                        "stops": [[0, "#999999"], [1, "#66cc00"], [2, "#ff9900"], [3, "#cc0000"]],
                        "property": "status"
                    },
                    "line-width": roadCondLineWidths[i]
                },
                "filter": roadCondFilters[i]
            }, 'df1df249e09c4fc19f63fc98378b9e98');
        }
    },

    _initStyleLayers(map, backgroundId, pid, id, property, legend, source, sourceLayer) {
        const roadCondLineWidths = [{ "stops": [[5, 1], [18, 4]], "base": 1.2 }, {
            "stops": [[5, 1], [18, 3]],
            "base": 1.2
        }, { "stops": [[5, 1], [18, 2.5]], "base": 1.2 }, { "stops": [[5, 1], [18, 2]], "base": 1.2 }]
        const roadCondFilters = [["in", "function_class", 1, 2], ["==", "function_class", 3], ["==", "function_class", 4], ["==", "function_class", 5]];
        const roadCondMaxZooms = [17.5, 17.5, 17.5, 17.5];
        const roadCondMinZooms = [5.0, 11.0, 13.0, 14.0];

        for (let i = 0; i < 4; i++) {
            map.addLayer({
                "id": id + parseInt(i + 1),
                "type": "line",
                "source": source,
                "source-layer": sourceLayer,
                "maxzoom": roadCondMaxZooms[i],
                "minzoom": roadCondMinZooms[i],
                "layout": {
                    "line-join": "round",
                    "line-cap": "round",
                    "visibility": "visible",
                },
                "paint": {
                    "line-color": {
                        "stops": legend,
                        "property": property
                    },
                    "line-width": roadCondLineWidths[i]
                },
                // "filter": roadCondFilters[i]
            }, backgroundId);
        }
    },

    _initWeatherVismLayers(map, backgroundId, pid) {  //能见度
        map.addLayer({
            "id": "vism",                //rh
            "type": "line",
            "source": "weather-all",            //weather
            "source-layer": "RoadWeatherAll",  //RoadWeather
            "layout": {
                "line-join": "round",
                "line-cap": "round",
                // 'visibility': 'none'    //
            },
            "paint": {
                "line-color": {
                    "property": "Vism",         //RH
                    "stops": [
                        // [0, '#28AD86'],
                        // [10, '#28AD86'],
                        // [10.1, '#E0B62C'],
                        // [20.8, '#E0B62C'],
                        // [20.9, '#FE4B3C'],
                        // [28.5, '#FE4B3C'],
                        // [28.6, '#8E71FF'],
                        [0.5, '#A189FF'],
                        [1, '#FE4B3C'],
                        [10, '#F8CA37'],
                        [50, '#28AD86']
                    ]
                },
                "line-width": 3
            }
        }, backgroundId);

    }

};

export default MapCtrl;